#ifndef DATABASE_H
#define DATABASE_H

#include <QDate>
#include <QDateTime>
#include <QDebug>
#include <QMap>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QVariantMap>
#include <QVector>

class Database
{
public:
    static void initTable();

    // 按日期查询数据
    static QVector<QVariantMap> queryByDate(const QDate &date);

    // 获取总记录数
    static int getTotalRecords(const QDate &date = QDate());

    // 分页查询数据
    static QVector<QVariantMap> queryByPage(int page, int pageSize, const QDate &date = QDate());

    // 插入整点数据
    static bool insertHourlyData(const QDateTime &timestamp,
                                 const QMap<QString, double> &deviceValues);

    // 校准和核查记录相关
    static bool insertInspectionRecord(const QString &deviceName, const QString &recordType,
                                       double standardConcentration, double measuredConcentration,
                                       const QDateTime &measuredTime);

    static bool insertCalibrationRecord(const QString &deviceName, const QString &calibType,
                                        double standardConcentration, double signalValue,
                                        const QDateTime &calibrationTime, double absorbance);

    // 加标回收记录相关
    static bool insertSpikeRecoveryRecord(const QString &deviceName, double originalConcentration,
                                          double spikedConcentration, double recoveryRate,
                                          const QDateTime &testTime);

    // 加标回收记录查询
    static QVector<QVariantMap> querySpikeRecoveryRecords(const QDate &date = QDate(),
                                                          const QString &deviceName = QString());
    static QVector<QVariantMap> querySpikeRecoveryRecordsByPage(
        int page, int pageSize, const QDate &date = QDate(), const QString &deviceName = QString());
    static int getSpikeRecoveryRecordsCount(const QDate &date = QDate(),
                                            const QString &deviceName = QString());

    // 核查记录查询
    static QVector<QVariantMap> queryInspectionRecords(const QDate &date = QDate(),
                                                       const QString &deviceName = QString());
    static QVector<QVariantMap> queryInspectionRecordsByPage(int page, int pageSize,
                                                             const QDate &date = QDate(),
                                                             const QString &deviceName = QString());
    static int getInspectionRecordsCount(const QDate &date = QDate(),
                                         const QString &deviceName = QString());

    // 校准记录查询
    static QVector<QVariantMap> queryCalibrationRecords(const QDate &date = QDate(),
                                                        const QString &deviceName = QString());
    static QVector<QVariantMap> queryCalibrationRecordsByPage(
        int page, int pageSize, const QDate &date = QDate(), const QString &deviceName = QString());
    static int getCalibrationRecordsCount(const QDate &date = QDate(),
                                          const QString &deviceName = QString());

private:
    Database() = delete;
    ~Database() = delete;
    Database(const Database &) = delete;
    Database &operator=(const Database &) = delete;
};

#endif  // DATABASE_H
